/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

import java.net.*;
import java.util.ArrayList;
import DLL.*;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import javax.swing.JOptionPane;

/**
 *
 * @author PyyPyLyn
 */
public class Upload_Core extends Thread
{
    
    public String TenFile;
    Integer Port;
    ArrayList<byte[]> bytesArray;    
    byte[] data;
    InetAddress Dest;
  
    
    public Upload_Core(byte[] dat, InetAddress ip) 
    {
        data = dat;
        Dest = ip;
        TenFile = new String("");
        Port = new Integer(0);
        bytesArray = new ArrayList<byte[]>();
        
    }

    
    public void run() 
    {
        bytesArray = ResponseFunction.ResponseDownloadRequest(data);        
        String str = ResponseFunction.LayThongTinGoiTin(data);
        
        TenFile = str.split(",")[0];
        Port = Integer.parseInt(str.split(",")[1]);
        
        int bytecount = bytesArray.size();
        int[] checksum = new int[bytecount];
        int[] acks = new int[bytecount];
        int[] seqs = new int[bytecount];
        ArrayList<byte[]> sendData = new ArrayList<byte[]>();
        int sequence = 0;
        int acknowlege = bytesArray.get(0).length - 1;
        for(int i = 0; i < bytecount; i++)
        {
            acks[i] = acknowlege;
            seqs[i] = sequence;
            checksum[i] = (int)(CommonMethods.calculateChecksum(bytesArray.get(i)));
            byte[] seData = ChangePacket.ChangePackets("0", checksum[i], seqs[i], acks[i], TenFile, bytesArray.get(i));
            sendData.add(seData);
            acknowlege += bytesArray.get(i).length;
            sequence = acknowlege + 1;
        }
        
        if(bytesArray != null)
        {                    
            try
            {
                
                Thread.sleep(1000);
            }
            catch (Exception ex)
            {
            
            }
            DatagramSocket d_Socket = null;
            DatagramSocket r_Socket = null;
            try
            {
                d_Socket = new DatagramSocket();
                r_Socket = new DatagramSocket(4449);
            }catch (Exception ex)
            {}
                //r_Socket.setSoTimeout(3000);
                boolean flag = false;
                
                byte[] r_pck = new byte[1024];
                ByteArrayInputStream bais = new ByteArrayInputStream(r_pck);
                DataInputStream dis = new DataInputStream(bais);
                DatagramPacket R_pk = new DatagramPacket(r_pck,r_pck.length);
                try
                {
                    for(int i=0;i<bytesArray.size();i++)
                    {
                            boolean result = SendFunction.SendPacketData(d_Socket, Dest, sendData.get(i), Port);
                            r_Socket.receive(R_pk);
                            flag = true;

    //                    try
    //                    {
    //                        while(true)
    //                        {

    //                            char a = dis.readChar();
    //                            if(a == '4')
    //                            {
    //                                byte[] seq = new byte[4];
    //                                seq = Arrays.copyOfRange(r_pck, 1, 5);
    //                                int seqnumber = ((seq[0] & 0xFF) << 24) | ((seq[1] & 0xFF) << 16)
    //                                                    | ((seq[2] & 0xFF) << 8) | (seq[3] & 0xFF);
    //                                seq = new byte[2];
    //                                seq = Arrays.copyOfRange(r_pck, 5, 7);
    //                                short headerlength = ByteBuffer.wrap(seq).order(ByteOrder.LITTLE_ENDIAN).getShort();
    //                                seq = new byte[headerlength];
    //                                seq = Arrays.copyOfRange(r_pck, 7, 7 + headerlength);
    //                                String header = new String(seq);
    //                                if(header == TenFile)
    //                                {
    //                                    if(seqnumber == sequence)
    //                                    {
                        
    //                                        break;                                        
    //                                    }
    //                                }                                
    //                            }                            
    //                        }                        
    //                    }
    //                    catch(Exception ex)
    //                    {
    //                        flag = false;
    //                        continue;
    //                    }
                    }  
                
                }
                catch (Exception ex)
                {

                }  
                finally
                {
                    d_Socket.close();
                    r_Socket.close();
                }
            
            try
            {
                WriteLog.WriteLog(TenFile, true, Dest.getHostAddress());
            }
            catch(Exception ax)
            {

            }
        }
        else // Gui goi tin tu choi' upload ve PC download
        {
            byte[] seData = QueryPackets.SendNormal("4", TenFile);
            
        }
    }

   

    
    
    
    
}
